import re
from rest_framework import serializers
from apps.users.models import UserProfile,VerifyCode


class UsersSerializer(serializers.ModelSerializer):
    # name = serializers.CharField(required=True)
    # moblie = serializers.CharField(required=True)

    class Meta:
        model = UserProfile
        #fields = "__all__"
        exclude = ('add_time',)


# class SmsSerializer(serializers.Serializer):
#     mobile = serializers.CharField(max_length=11)
#     userId = serializers.CharField(max_length=11)
#
#     User = UserProfile.objects.get(pk = userId)
#     def validate_mobile(self,mobile):
#         """
#         验证手机号码（函数名称必须为validate+字段名）
#         :param mobile:
#         :return:
#         """
#
#         # 手机是否注册
#         if User.objects.filter(mobile=mobile).count():
#             raise serializers.ValidationError("用户已经存在")
#
#         # 验证手机号码是否合法
#         if not re.match(REGEX_MOBILE,mobile):
#             raise serializers.ValidationError("手机号非法")
#
#         # 验证码发送频率
#         one_mintes_age = datetime.now() - timedelta(hours=0,minutes=1,seconds=0)
#         # 添加时间大于一分钟以前，也就是距离现在还不足一分钟
#         if VerifyCode.objects.filter(add_time__gt=one_mintes_age,mobile=mobile).count():
#             raise serializers.ValidationError("距离上一次发送未超过60s")
#
#         return mobile